//------------------------------------------------------------------------------ 
// <copyright file="HeaderTabTemplate.cs" company="Fm Computer">
//     Copyright (c) Fm Computer di Menatti Fulvia.  All rights reserved.
// </copyright>
//----------------------------------------------------------------------------- 
namespace FmjQuery
{
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    /// <summary>
    /// Header Tab Template
    /// </summary>
    internal sealed class HeaderTabTemplate : ITemplate
    {
        #region Field
        /// <summary>
        /// Name of title label
        /// </summary>
        private const string TITLE = "lbTitle";
        #endregion

        #region Implementation of ITemplate
        /// <summary>
        /// When implemented by a class, defines the System.Web.UI.Control object that child controls and templates belong to. These child controls are in turn defined within an inline template.
        /// </summary>
        /// <param name="container">
        /// he System.Web.UI.Control object to contain the instances of controls from the inline template.
        /// </param>
        public void InstantiateIn(Control container) 
        {
            if (container == null)
            {
                throw new ArgumentNullException("container");
            }

            Literal lttitle = new Literal { ID = TITLE, Text = "<li><a href='{0}'><span>{1}</span></a></li>" };
            lttitle.DataBinding += DataBind;

            container.Controls.Add(lttitle);
        }
        #endregion

        #region Private Method
        /// <summary>
        /// Binds a data source to the invoked server control and all its child controls.
        /// </summary>
        /// <param name="sender">Control that create event</param>
        /// <param name="e">arguments event</param>
        private static void DataBind(object sender, EventArgs e)
        {
            Control c = (Control)sender;
            JTab tab = (JTab)c.NamingContainer;

            if (c.ID.Equals(TITLE))
            {
                Literal lt = (Literal)c;
                lt.Text = string.Format(lt.Text, !tab.IsWebPage ? string.Concat("#tb_", tab.ClientID) : tab.Url, tab.HeaderText);
            }
        }
        #endregion
    }
}